#ifndef CONTACTMODEL_H
#define CONTACTMODEL_H

#include <QObject>
#include <QQmlListProperty>
#include <QXmppVCardIq.h>

#include "../../global/xnamespace.h"
#include "VCard.h"

X_BEGIN_NAMESPACE

// 私有数据，应该放到CPP文件中，目前因为编译时模板的原因，暂时放在头文件中
class ContactModelPrivate : public QSharedData {
public:
    ContactModelPrivate() { m_vcard = NULL; }
    virtual ~ContactModelPrivate(){
        delete m_vcard;
    }

public:
    AvailableStatus m_availableStatus;      // 在线状态
    QString m_bareJid;                      // bareJid
    QString m_nickName;
    QString m_remarkName;
    QString m_group;                        // 所在分组
    QString m_description;
    QString m_photo;
    QString m_thumbnail;
    PhotoType m_photoType;

    VCard *m_vcard;
};

/**
 * @brief 联系人
 *
 * 联系人模型用于在视图上显示。大部分信息从QXmppVCardIq中获取，除了联系人的Jid, 在线状态，联系人分组等简单的信息在Contact中管理。
 */
class ContactModel : public QObject {
    Q_OBJECT

    Q_PROPERTY(X::AvailableStatus availableStatus READ availableStatus WRITE setAvailableStatus NOTIFY availableStatusChanged)
    Q_PROPERTY(QString bareJid READ bareJid NOTIFY bareJidChanged)
    Q_PROPERTY(QString nickName READ nickName WRITE setNickName NOTIFY nickNameChanged)
    Q_PROPERTY(QString remarkName READ remarkName WRITE setRemarkName NOTIFY remarkNameChanged)
    Q_PROPERTY(QString group READ group WRITE setGroup NOTIFY groupChanged)
    Q_PROPERTY(QDate birthday READ birthday WRITE setBirthday NOTIFY birthdayChanged)
    Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged)
    Q_PROPERTY(QString telephone READ telephone WRITE setTelephone NOTIFY telephoneChanged)
    Q_PROPERTY(QString mobile READ mobile WRITE setMobile NOTIFY mobileChanged)
    Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY emailChanged)
    Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged)
    Q_PROPERTY(QString photo READ photo WRITE setPhoto NOTIFY photoChanged)
    Q_PROPERTY(QString thumbnail READ thumbnail NOTIFY thumbnailChanged)
    Q_PROPERTY(PhotoType photoType READ photoType WRITE setPhotoType NOTIFY photoTypeChanged)
    Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)

signals:
    void availableStatusChanged(X::AvailableStatus availableStatus);
    void bareJidChanged(QString bareJid);
    void groupChanged(QString group);
    void birthdayChanged(QDate birthday);
    void descriptionChanged(QString description);
    void emailChanged(QString email);
    void remarkNameChanged(QString remarkName);
    void nickNameChanged(QString nickName);
    void photoChanged(QString photo);
    void photoTypeChanged(PhotoType photoType);
    void urlChanged(QString url);
    void addressChanged(QString address);
    void telephoneChanged(QString telephone);
    void mobileChanged(QString mobile);

    void thumbnailChanged(QString thumbnail);

public:
    ContactModel();
    ContactModel(const QString& bareJid);
    ContactModel(const ContactModel& src);
    ContactModel &operator =(const ContactModel& rhs);

    void loadVCard();
    void saveVCard();
    void updateFromXmppIq(const QXmppVCardIq& iq);

    VCard &vcard() const;

public:
    const static char* qmlName();

public:
    X::AvailableStatus availableStatus() const;
    QString bareJid() const;
    QString group() const;
    QDate birthday() const;
    QString description() const;
    QString telephone() const;
    QString mobile() const;
    QString email() const;
    QString address() const;
    QString remarkName() const;
    QString nickName() const;
    QString photo() const;
    PhotoType photoType() const;
    QString url() const;
    QString thumbnail() const;

public slots:
    ContactModel &setAvailableStatus(X::AvailableStatus availableStatus);
    ContactModel &setGroup(QString group);
    ContactModel &setBirthday(QDate birthday);
    ContactModel &setDescription(QString description);
    ContactModel &setTelephone(QString telephone);
    ContactModel &setMobile(QString mobile);
    ContactModel &setEmail(QString email);
    ContactModel &setAddress(QString address);
    ContactModel &setRemarkName(QString remarkName);
    ContactModel &setNickName(QString nickName);
    ContactModel &setPhoto(QString photo);
    ContactModel &setPhotoType(PhotoType photoType);
    ContactModel &setUrl(QString url);

private:
    mutable QSharedDataPointer<ContactModelPrivate> d;
};

X_END_NAMESPACE

#endif // CONTACTMODEL_H
